﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace VfpEntityFrameworkProvider.Tests {
    [TestClass]
    public class XmlToCursorTests : TestBase {
        [TestMethod]
        public void NestedJoinWithXmlToCursorRewriteTest() {
            var northwind = this.GetContext();
            var query = northwind.OrderDetails
                                 .Join(northwind.Orders, x => x.OrderID, x => x.OrderID, (o, d) => new { Order = o, OrderDetail = d })
                                 .Join(northwind.Products, x => x.Order.ProductID, x => x.ProductID, (x, p) => new { Order = x.Order, OrderDetail = x.OrderDetail, Product = p })
                                 .Where(x => CustomerIds.Contains(x.OrderDetail.Customer.CustomerID))
                                 .Select(x => new {
                                     OrderID = x.Order.OrderID,
                                     ProductID = x.Product.ProductID,
                                     Freight = x.OrderDetail.Freight
                                 });
            query.ToList();
        }

        [TestMethod]
        public void TestXmlToCursorMultiple() {
            var northwind = GetContext();
            var orderIds = OrderIds.Take(160).ToArray();
            var customerIds = CustomerIds.ToArray();

            var list = northwind.Orders
                                .Where(order => orderIds.Contains(order.OrderID) && customerIds.Contains(order.Customer.CustomerID))
                                .ToList();

            Assert.AreEqual(160, list.Count);
        }

        [TestMethod]
        public void TestXmlToCursorAndIn() {
            var northwind = GetContext();
            var ids = OrderIds.Take(160).ToArray();
            var list = northwind.Orders
                                .Where(x => x.OrderID > 0)
                                .Where(order => ids.Contains(order.OrderID))
                                .ToList();

            Assert.AreEqual(160, list.Count);
        }

        [TestMethod]
        public void TestXmlToCursorNotIn() {
            var northwind = GetContext();
            var ids = OrderIds.Take(160).ToArray();
            var list = northwind.Orders.Where(order => !ids.Contains(order.OrderID)).ToList();

            Assert.AreEqual(670, list.Count);
        }

        [TestMethod]
        public void TestXmlToCursorOrIn() {
            var northwind = GetContext();
            var ids1 = OrderIds.Take(50).ToArray();
            var ids2 = CustomerIds.Take(50).ToArray();
            var list = northwind.Orders
                                .Where(order => ids1.Contains(order.OrderID) || ids2.Contains(order.Customer.CustomerID))
                                .ToList();

            Assert.AreEqual(483, list.Count);
        }

        [TestMethod]
        public void TestXmlToCursor() {
            var northwind = GetContext();
            var ids = ProductNames.Take(30);
            var list = northwind.Products.Where(o => ids.Contains(o.ProductName)).ToList();

            Assert.AreEqual(30, list.Count);
        }

        private static readonly IEnumerable<int> OrderIds = new[] {
            10248, 10249, 10250, 10251, 10252, 10253, 10254, 10255, 10256, 10257, 10258, 10259, 10260, 10261, 10262, 10263, 10264, 10265, 10266, 10267, 10268, 10269, 10270, 10271, 10272, 10273, 10274, 10275, 10276, 10277, 10278, 10279,
            10280, 10281, 10282, 10283, 10284, 10285, 10286, 10287, 10288, 10289, 10290, 10291, 10292, 10293, 10294, 10295, 10296, 10297, 10298, 10299, 10300, 10301, 10302, 10303, 10304, 10305, 10306, 10307, 10308, 10309, 10310, 10311,
            10312, 10313, 10314, 10315, 10316, 10317, 10318, 10319, 10320, 10321, 10322, 10323, 10324, 10325, 10326, 10327, 10328, 10329, 10330, 10331, 10332, 10333, 10334, 10335, 10336, 10337, 10338, 10339, 10340, 10341, 10342, 10343,
            10344, 10345, 10346, 10347, 10348, 10349, 10350, 10351, 10352, 10353, 10354, 10355, 10356, 10357, 10358, 10359, 10360, 10361, 10362, 10363, 10364, 10365, 10366, 10367, 10368, 10369, 10370, 10371, 10372, 10373, 10374, 10375,
            10376, 10377, 10378, 10379, 10380, 10381, 10382, 10383, 10384, 10385, 10386, 10387, 10388, 10389, 10390, 10391, 10392, 10393, 10394, 10395, 10396, 10397, 10398, 10399, 10400, 10401, 10402, 10403, 10404, 10405, 10406, 10407,
            10408, 10409, 10410, 10411, 10412, 10413, 10414, 10415, 10416, 10417, 10418, 10419, 10420, 10421, 10422, 10423, 10424, 10425, 10426, 10427, 10428, 10429, 10430, 10431, 10432, 10433, 10434, 10435, 10436, 10437, 10438, 10439,
            10440, 10441, 10442, 10443, 10444, 10445, 10446, 10447, 10448, 10449, 10450, 10451, 10452, 10453, 10454, 10455, 10456, 10457, 10458, 10459, 10460, 10461, 10462, 10463, 10464, 10465, 10466, 10467, 10468, 10469, 10470, 10471,
            10472, 10473, 10474, 10475, 10476, 10477, 10478, 10479, 10480, 10481, 10482, 10483, 10484, 10485, 10486, 10487, 10488, 10489, 10490, 10491, 10492, 10493, 10494, 10495, 10496, 10497, 10498, 10499, 10500, 10501, 10502, 10503,
            10504, 10505, 10506, 10507, 10508, 10509, 10510, 10511, 10512, 10513, 10514, 10515, 10516, 10517, 10518, 10519, 10520, 10521, 10522, 10523, 10524, 10525, 10526, 10527, 10528, 10529, 10530, 10531, 10532, 10533, 10534, 10535,
            10536, 10537, 10538, 10539, 10540, 10541, 10542, 10543, 10544, 10545, 10546, 10547, 10548, 10549, 10550, 10551, 10552, 10553, 10554, 10555, 10556, 10557, 10558, 10559, 10560, 10561, 10562, 10563, 10564, 10565, 10566, 10567,
            10568, 10569, 10570, 10571, 10572, 10573, 10574, 10575, 10576, 10577, 10578, 10579, 10580, 10581, 10582, 10583, 10584, 10585, 10586, 10587, 10588, 10589, 10590, 10591, 10592, 10593, 10594, 10595, 10596, 10597, 10598, 10599,
            10600, 10601, 10602, 10603, 10604, 10605, 10606, 10607, 10608, 10609, 10610, 10611, 10612, 10613, 10614, 10615, 10616, 10617, 10618, 10619, 10620, 10621, 10622, 10623, 10624, 10625, 10626, 10627, 10628, 10629, 10630, 10631,
            10632, 10633, 10634, 10635, 10636, 10637, 10638, 10639, 10640, 10641, 10642, 10643, 10644, 10645, 10646, 10647, 10648, 10649, 10650, 10651, 10652, 10653, 10654, 10655, 10656, 10657, 10658, 10659, 10660, 10661, 10662, 10663,
            10664, 10665, 10666, 10667, 10668, 10669, 10670, 10671, 10672, 10673, 10674, 10675, 10676, 10677, 10678, 10679, 10680, 10681, 10682, 10683, 10684, 10685, 10686, 10687, 10688, 10689, 10690, 10691, 10692, 10693, 10694, 10695,
            10696, 10697, 10698, 10699, 10700, 10701, 10702, 10703, 10704, 10705, 10706, 10707, 10708, 10709, 10710, 10711, 10712, 10713, 10714, 10715, 10716, 10717, 10718, 10719, 10720, 10721, 10722, 10723, 10724, 10725, 10726, 10727,
            10728, 10729, 10730, 10731, 10732, 10733, 10734, 10735, 10736, 10737, 10738, 10739, 10740, 10741, 10742, 10743, 10744, 10745, 10746, 10747, 10748, 10749, 10750, 10751, 10752, 10753, 10754, 10755, 10756, 10757, 10758, 10759,
            10760, 10761, 10762, 10763, 10764, 10765, 10766, 10767, 10768, 10769, 10770, 10771, 10772, 10773, 10774, 10775, 10776, 10777, 10778, 10779, 10780, 10781, 10782, 10783, 10784, 10785, 10786, 10787, 10788, 10789, 10790, 10791,
            10792, 10793, 10794, 10795, 10796, 10797, 10798, 10799, 10800, 10801, 10802, 10803, 10804, 10805, 10806, 10807, 10808, 10809, 10810, 10811, 10812, 10813, 10814, 10815, 10816, 10817, 10818, 10819, 10820, 10821, 10822, 10823,
            10824, 10825, 10826, 10827, 10828, 10829, 10830, 10831, 10832, 10833, 10834, 10835, 10836, 10837, 10838, 10839, 10840, 10841, 10842, 10843, 10844, 10845, 10846, 10847, 10848, 10849, 10850, 10851, 10852, 10853, 10854, 10855,
            10856, 10857, 10858, 10859, 10860, 10861, 10862, 10863, 10864, 10865, 10866, 10867, 10868, 10869, 10870, 10871, 10872, 10873, 10874, 10875, 10876, 10877, 10878, 10879, 10880, 10881, 10882, 10883, 10884, 10885, 10886, 10887,
            10888, 10889, 10890, 10891, 10892, 10893, 10894, 10895, 10896, 10897, 10898, 10899, 10900, 10901, 10902, 10903, 10904, 10905, 10906, 10907, 10908, 10909, 10910, 10911, 10912, 10913, 10914, 10915, 10916, 10917, 10918, 10919,
            10920, 10921, 10922, 10923, 10924, 10925, 10926, 10927, 10928, 10929, 10930, 10931, 10932, 10933, 10934, 10935, 10936, 10937, 10938, 10939, 10940, 10941, 10942, 10943, 10944, 10945, 10946, 10947, 10948, 10949, 10950, 10951,
            10952, 10953, 10954, 10955, 10956, 10957, 10958, 10959, 10960, 10961, 10962, 10963, 10964, 10965, 10966, 10967, 10968, 10969, 10970, 10971, 10972, 10973, 10974, 10975, 10976, 10977, 10978, 10979, 10980, 10981, 10982, 10983,
            10984, 10985, 10986, 10987, 10988, 10989, 10990, 10991, 10992, 10993, 10994, 10995, 10996, 10997, 10998, 10999, 11000, 11001, 11002, 11003, 11004, 11005, 11006, 11007, 11008, 11009, 11010, 11011, 11012, 11013, 11014, 11015,
            11016, 11017, 11018, 11019, 11020, 11021, 11022, 11023, 11024, 11025, 11026, 11027, 11028, 11029, 11030, 11031, 11032, 11033, 11034, 11035, 11036, 11037, 11038, 11039, 11040, 11041, 11042, 11043, 11044, 11045, 11046, 11047,
            11048, 11049, 11050, 11051, 11052, 11053, 11054, 11055, 11056, 11057, 11058, 11059, 11060, 11061, 11062, 11063, 11064, 11065, 11066, 11067, 11068, 11069, 11070, 11071, 11072, 11073, 11074, 11075, 11076, 11077
        };

        private static readonly IEnumerable<string> CustomerIds = new[] {
            "ALFKI", "ANATR", "ANTON", "AROUT", "BERGS", "BLAUS", "BLONP", "BOLID", "BONAP", "BOTTM", "BSBEV", "CACTU", "CENTC", "CHOPS", "COMMI", "CONSH", "DRACD", "DUMON", "EASTC", "ERNSH", "FAMIA", "FISSA", "FOLIG", "FOLKO",
            "FRANK", "FRANR", "FRANS", "FURIB", "GALED", "GODOS", "GOURL", "GREAL", "GROSR", "HANAR", "HILAA", "HUNGC", "HUNGO", "ISLAT", "KOENE", "LACOR", "LAMAI", "LAUGB", "LAZYK", "LEHMS", "LETSS", "LILAS", "LINOD", "LONEP",
            "MAGAA", "MAISD", "MEREP", "MORGK", "NORTS", "OCEAN", "OLDWO", "OTTIK", "PARIS", "PERIC", "PICCO", "PRINI", "QUEDE", "QUEEN", "QUICK", "RANCH", "RATTC", "REGGC", "RICAR", "RICSU", "ROMEY", "SANTG", "SAVEA", "SEVES",
            "SIMOB", "SPECD", "SPLIR", "SUPRD", "THEBI", "THECR", "TOMSP", "TORTU", "TRADH", "TRAIH", "VAFFE", "VICTE", "VINET", "WANDK", "WARTH", "WELLI", "WHITC", "WILMK", "WOLZA"
        };

        private static readonly IEnumerable<string> ProductNames = new[] { 
            "Alice Mutton", "Aniseed Syrup", "Boston Crab Meat", "Camembert Pierrot", "Carnarvon Tigers", "Chai", "Chang", "Chartreuse verte", "Chef Anton's Cajun Seasoning", "Chef Anton's Gumbo Mix", "Chocolade", "Côte de Blaye",
            "Escargots de Bourgogne", "Filo Mix", "Flotemysost", "Geitost", "Genen Shouyu", "Gnocchi di nonna Alice", "Gorgonzola Telino", "Grandma's Boysenberry Spread", "Gravad lax", "Guaran  Fant stica", "Gudbrandsdalsost",
            "Gula Malacca", "Gumbär Gummibärchen", "Gustaf's Knäckebröd", "Ikura", "Inlagd Sill", "Ipoh Coffee", "Jack's New England Clam Chowder", "Konbu", "Lakkalikööri", "Laughing Lumberjack Lager", "Longlife Tofu",
            "Louisiana Fiery Hot Pepper Sauce", "Louisiana Hot Spiced Okra", "Manjimup Dried Apples", "Mascarpone Fabioli", "Maxilaku", "Mishi Kobe Niku", "Mozzarella di Giovanni", "Nord-Ost Matjeshering",
            "Northwoods Cranberry Sauce", "NuNuCa Nuá-Nougat-Creme", "Original Frankfurter grüne Soáe", "Outback Lager", "Pavlova", "Perth Pasties", "Pâté chinois", "Queso Cabrales", "Queso Manchego La Pastora", "Raclette Courdavault",
            "Ravioli Angelo", "Rhönbräu Klosterbier", "Rogede sild", "Röd Kaviar", "Rössle Sauerkraut", "Sasquatch Ale", "Schoggi Schokolade", "Scottish Longbreads", "Singaporean Hokkien Fried Mee", "Sir Rodney's Marmalade",
            "Sir Rodney's Scones", "Sirop d'érable", "Spegesild", "Steeleye Stout", "Tarte au sucre", "Teatime Chocolate Biscuits", "Thüringer Rostbratwurst", "Tofu", "Tourtière", "Tunnbröd", "Uncle Bob's Organic Dried Pears", "Valkoinen suklaa",
            "Vegie-spread", "Wimmers gute Semmelknödel", "Zaanse koeken"
        };
    }
}